#include "Game.h"

Game::Game()
{
	m_running = true;
}

Game::~Game()
{
	release();
}

void Game::init(WindowHandle* a_handle)
{
	//seed random
	srand((UINT)time(0));

	//framework init
	m_app.init("Direct3D",CL_XBYTE(9,20,27),WINW,WINH,true,a_handle);
	m_fps.init();
	m_time.init();
	m_d3d.init(&m_app,false);
	m_din.init(&m_app);

	//statemachine init
	m_system.queueCartridge(new GameMenu(FRAMEWORKS_MEM_REF));
	m_system.queueCartridge(new GameGalaxy(FRAMEWORKS_MEM_REF));
	m_system.queueCartridge(new GameWin(FRAMEWORKS_MEM_REF));
	m_system.queueCartridge(new GameLose(FRAMEWORKS_MEM_REF));
	m_system.queueCartridge(new GameCredit(FRAMEWORKS_MEM_REF));

	//statemachine system data
	m_systemData.init();
	m_system.systemData(&m_systemData);

	for(int i = 0; i < m_systemData.m_quadAmt; i++)
	{
		char name[16]; memset(name,0,16);
		sprintf_s(name,"GameQuadrant%d",i);
		m_system.queueCartridge(new GameQuadrant(FRAMEWORKS_MEM_REF,name,GAMEQUADRANT+i));
	}

	m_system.powerOffId(GAMEQUIT);
	m_system.powerOnSystem();
}

void Game::reset()
{
	m_system.reset();
}

void Game::release()
{
	//system data release
	m_systemData.release();

	//statemachine release
	m_system.powerOffSystem();

	//framework release
	m_d3d.release();
	m_din.release();
}

void Game::input()
{
	m_system.input();
}

void Game::update()
{
	m_system.update();
}

void Game::draw()
{
	m_system.draw();
}

void Game::pre()
{
	m_system.pre();
}

void Game::post()
{
	m_system.post();
	m_system.checkCartridge();
}

bool Game::running()
{
	return (m_running && m_app.hMsg() && m_system.isPowered())?true:false;
}